require "Common/define"
require "Logic/Config"
require "Common/functions"
UIRankingShow = {};
HelperFunc.SetUIEnv(UIRankingShow)
local this = UIRankingShow
local use_lihui = true
function UIRankingShow.Init()
    local name, packageName = getUIName(PanelNames.UIRankingShow);
    panelMgr:Push(name, packageName, this.onCreate, Config.useLocalUI);
end

function UIRankingShow.onCreate(obj)
    if this.have_init ~= true then
        this.have_init = true
        this.show_controller = obj:GetController("Show")
        this.btn_bg_close = obj:GetChild("bgClose")
        this.btn_back = obj:GetChild("back")
        this.btn_switch = obj:GetChild("switchBtn")
        this.cur_switch_index = 1
        this.txt_time = obj:GetChild("Time")
        this.txt_desc = obj:GetChild("Desc")
        this.txt_name = obj:GetChild("Name")
        this.draw_wrapper = GoWrapper.New()
        this.draw_lihui = obj:GetChild("draw")
        this.draw_picture = obj:GetChild("Picture")

        this.draw_small_wrapper = GoWrapper.New()
        this.draw_small_lihui = obj:GetChild("draw_small")

        this.addEvent()

        this.txt_name.text = getLanguage("UIRanking08")
        this.txt_desc.text = getLanguage("UIRanking09")
        
        this.refreshData() 
        this.show_controller:SetSelectedIndex(0)
        -- if use_lihui then
            this.updateLiHui()
        -- else
            -- this.updateIcon()
        -- end
    end

end

function UIRankingShow.updateIcon()
    -- this.show_controller:SetSelectedIndex(1)
    this.draw_picture.url = "ui://UIRanking/Picture_1"
    if this.cur_switch_index == 1 then
    else
    end
end

function UIRankingShow.updateLiHui()
    -- this.show_controller:SetSelectedIndex(0)
 
    local cfg_item_str = G.dataTable["t_global"]["450"].f_string_value
    local cfg_item = string.split(cfg_item_str, ",") 
    local path = tostring(cfg_item[1])
    local scale_x = tonumber(cfg_item[2])
    local scale_y = tonumber(cfg_item[3])
    local location_x = tonumber(cfg_item[4])
    local location_y = tonumber(cfg_item[5])
 
    resMgr:LoadModelPrefab(path, function(go)
        if this.have_init ~= true then return end
        if this.RoleLihui~=nil then
            destroyImmediate(this.RoleLihui)
            this.RoleLihui = nil
        end
        this.RoleLihui = newObject(go)
        this.RoleLihui:AddComponent(TCanvas)
        this.draw_wrapper:SetWrapTarget(this.RoleLihui,false)
        Util.Lua_SetLocalPositionXYZ(this.RoleLihui.transform, location_x, location_y, 0)
        Util.Lua_SetLocalScaleXYZ(this.RoleLihui.transform, scale_x*this.draw_lihui.scaleX, scale_y*this.draw_lihui.scaleY, 1) 
        local skeleton_graphic = this.RoleLihui:GetComponent("SkeletonGraphic")
        if skeleton_graphic ~= nil then
            skeleton_graphic.AnimationState:SetAnimation(0, "idle1", true);
        end
        this.draw_lihui:SetNativeObject(this.draw_wrapper)
    end)

    local cfg_item_str1 = G.dataTable["t_global"]["451"].f_string_value
    local cfg_item1 = string.split(cfg_item_str1, ",") 
    local path1 = tostring(cfg_item1[1])
    local scale_x1 = tonumber(cfg_item1[2])
    local scale_y1 = tonumber(cfg_item1[3])
    local location_x1 = tonumber(cfg_item1[4])
    local location_y1 = tonumber(cfg_item1[5])
    local heroId = tonumber(cfg_item1[6])
    resMgr:LoadModelPrefab(path1, function(go)
        if this.have_init ~= true then return end
        if this.RoleLihui2~=nil then
            destroyImmediate(this.RoleLihui2)
            this.RoleLihui2 = nil
        end
        this.RoleLihui2 = newObject(go)
        this.RoleLihui2:AddComponent(TCanvas)
        this.draw_small_wrapper:SetWrapTarget(this.RoleLihui2, false)
        Util.Lua_SetLocalPositionXYZ(this.RoleLihui2.transform, location_x1, location_y1, 0)
        Util.Lua_SetLocalScaleXYZ(this.RoleLihui2.transform, scale_x1*this.draw_small_lihui.scaleX, scale_y1*this.draw_small_lihui.scaleY, 1) 
        local skeleton_graphic = this.RoleLihui2:GetComponent("SkeletonGraphic")
        if skeleton_graphic ~= nil then
            skeleton_graphic.AnimationState:SetAnimation(0, "idle1", true);
        end
        local weaponObj = Util.Lua_FindGameObject(this.RoleLihui2, "Animation");
        logicMgr.EquipManager.setWeaponSkin(weaponObj, heroId, true);
        this.draw_small_lihui:SetNativeObject(this.draw_small_wrapper)
    end)
end

function UIRankingShow.CloseTimer()
    if this.activity_time_id ~= nil then
        utimer.remove(this.activity_time_id)
        this.activity_time_id = nil 
    end
end

function UIRankingShow.refreshData() 
    local server_time = Config.serverTime/1000 
    local end_time = logicMgr.NoticeManager.rank_balance_time
    local dis_time = end_time - server_time
    if dis_time > 0 then
        this.CloseTimer()
        this.txt_time.text = string.format(getLanguage("UIRanking10"), getTimeStrDHMS(dis_time))
        this.activity_time_id = utimer.timeCall(function() 
            dis_time = dis_time -1 
            if this.txt_time then
                local tips_str = string.format(getLanguage("UIRanking10"), getTimeStrDHMS(dis_time))
                if this.banner_tips_str == nil or this.banner_tips_str ~= tips_str then
                    this.txt_time.text =  tips_str
                    this.banner_tips_str = tips_str
                end
            end
            if dis_time <= 0 then 
                if this.txt_time then
                    this.txt_time.text = getLanguage("Sys_ActivityEnd")
                end
                this.CloseTimer()
            end
        end, 1000, dis_time) 
    end
end

function UIRankingShow.addEvent()  
    this.btn_back.onClick:Add( this.onClose)
    this.btn_bg_close.onClick:Add( this.onClose )
    this.btn_switch.onClick:Add( this.onClickSwitch )
end

function UIRankingShow.removeEvent()  
    if this.btn_back then
        this.btn_back.onClick:Clear()
    end
    if this.btn_bg_close then
        this.btn_bg_close.onClick:Clear()
    end
    if this.btn_switch then
        this.btn_switch.onClick:Clear()
    end
end

function UIRankingShow.onClose()
    closeUI(PanelNames.UIRankingShow)
end

function UIRankingShow.onClickSwitch()
    if this.cur_switch_index== 1 then
        this.cur_switch_index = 2
        this.show_controller:SetSelectedIndex(1)
    else
        this.cur_switch_index = 1
        this.show_controller:SetSelectedIndex(0)
    end
    -- this.updateLiHui()
    -- if use_lihui then
    --     this.updateLiHui()
    -- else
    --     this.updateIcon()
    -- end
end

function UIRankingShow.OnDestroy()
    this.removeEvent()
    if this.RoleLihui~=nil then
        destroyImmediate(this.RoleLihui);
        this.RoleLihui = nil
    end
    this.have_init = nil
    this.btn_back = nil
    this.draw_wrapper = nil
    this.draw_small_wrapper = nil
end

-- function UIRankingShow.OnEnable()
-- end
-- function UIRankingShow.OnDisable()
-- end


return UIRankingShow